package com.hllxd.hism.star.basic.service.dao.redis;

import com.hllxd.hism.star.basic.service.config.redis.RedisAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lixiyong01
 * @date 2021-08-21 11:29
 */
@Slf4j
@Service("yarnRedisMapper")
public class YarnRedisMapperImpl implements YarnRedisMapper {
    @Autowired private RedisAdapter redisAdapter;

    /** 有效时间15天 */
    private static final long EXPIRE_DATE = 15 * 24 * 60 * 60 * 1000L;

    private static final String CDATA_PREFIX = "zp_cdata_yarn^";


    private String buildKey2(String app, String queue) {
        return CDATA_PREFIX + app + "^" + queue;
    }

    private String buildKey(String key) {
        return CDATA_PREFIX + key;
    }

    @Override
    public void setRequestLastTs(String key, Long ts) {
        redisAdapter.setValue(buildKey(key), ts.toString(), EXPIRE_DATE);
    }

    @Override
    public Long getRequestLastTs(String key) {
        String val = redisAdapter.getValue(buildKey(key));
        try {
            return (val==null)?0L:Long.valueOf(val);
        }catch (Exception e) {
            log.error("Exception:{}, {}", val, e.toString());
            return 0L;
        }
    }

    @Override
    public void setQueue(String app, String queue, String json) {
        redisAdapter.setValue(buildKey2(app,queue), json, EXPIRE_DATE);
    }

    @Override
    public String getQueue(String app, String queue) {
        return redisAdapter.getValue(buildKey2(app,queue));
    }

}
